import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_pu_user_locator import AdminPUUserLocator
from Case.python_ui.Common.commontool import CommonTool
from selenium.common.exceptions import StaleElementReferenceException
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.PageLocate.admin_security_settings_locator import AdminSecuritySettingsLocator




class AdminSecuritySettingsPages(Basepage):

    #进入security settings页面
    def enter_sucurity_settings_page(self):
       time.sleep(2)
       doc = '进入security settings菜单'
       js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
       self.excute_js(js_object);

       self.wait_elevisble(AdminSecuritySettingsLocator.account_settings_menu, doc)
       self.click_element(AdminSecuritySettingsLocator.account_settings_menu, doc)
       self.wait_elevisble(AdminSecuritySettingsLocator.security_settings_menu, doc)
       self.click_element(AdminSecuritySettingsLocator.security_settings_menu, doc)


    def enter_pages_admin1(self):
        time.sleep(2)
        doc = '进入security settings菜单'
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object);
        self.wait_elevisble(AdminSecuritySettingsLocator.account_settings_menu, doc)
        self.click_element(AdminSecuritySettingsLocator.account_settings_menu, doc)
        self.wait_elevisble(AdminSecuritySettingsLocator.security_settings_menu, doc)
        self.click_element(AdminSecuritySettingsLocator.security_settings_menu, doc)


    #查看页面属性
    def view_security_settings_page(self):
        doc = '查看security settings页面字段属性'
        self.enter_sucurity_settings_page()
        self.wait_elevisble(AdminSecuritySettingsLocator.change_password, doc)
        change_password = (AdminSecuritySettingsLocator.change_password, doc)
        Authentication = (AdminSecuritySettingsLocator.Authentication, doc)
        return change_password, Authentication


    # admin1登录
    def login(self,cmdopt):
        doc='user_admin login'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(10)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        #验证码
        #self.wait_elevisble(Loginlocator.verification_code,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele,Logindata.user_data[cmdopt]['admin1']['username'],doc)
        self.send_keys(Loginlocator.login_password_ele,Logindata.user_data[cmdopt]['admin1']['password'],doc)
        #验证码
        #self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        #self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        self.click_element(Loginlocator.email_submit_ele,doc)
        self.wait_elevisble(Loginlocator.admin_home_page, doc)
        time.sleep(10)

    # 旧密码修改新密码
    def change_password(self):
        doc = '使用旧密码修改新密码'
        self.enter_pages_admin1()
        time.sleep(5)
        self.wait_elevisble(AdminSecuritySettingsLocator.change_password_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.change_password_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.old_password_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.old_password_input, Logindata.user_data['sit'] ['admin1'] ['password'], doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_input, Logindata.new_pwd, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_confirm_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_confirm_input, Logindata.confirm_pwd, doc)
        time.sleep(3)
        self.wait_elevisble(AdminSecuritySettingsLocator.confirm_botton0, doc)
        self.click_element(AdminSecuritySettingsLocator.confirm_botton0, doc)
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data['sit']['admin1']['username'], doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.new_pwd, doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc)
        self.wait_elevisble(Loginlocator.email_verification_code, doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code['sit']['code'], doc)
        self.click_element(Loginlocator.email_submit_ele, doc)
        self.wait_elevisble(Loginlocator.admin_home_page, doc)

        #改回原密码
        self.enter_pages_admin1()
        time.sleep(5)
        self.wait_elevisble(AdminSecuritySettingsLocator.change_password_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.change_password_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.old_password_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.old_password_input, Logindata.new_pwd, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_input, Logindata.user_data['sit']['admin1']['password'], doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_confirm_input, Logindata.user_data['sit']['admin1']['password'], doc)
        time.sleep(3)
        self.wait_elevisble(AdminSecuritySettingsLocator.confirm_botton0, doc)
        self.click_element(AdminSecuritySettingsLocator.confirm_botton0, doc)
        time.sleep(5)


    # 邮箱验证码修改密码
    def forget_password(self):
        doc = '忘记密码，使用email验证码修改密码'
        time.sleep(5)
        self.wait_elevisble(AdminSecuritySettingsLocator.change_password_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.change_password_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.forget_password_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.forget_password_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.verification_code_input, doc)
        # self.click_element(UserSecuritySettingsLocator.send_botton, doc)
        # code=BusinessCommonTools().get_verification_code(Logindata.user_data['sit']['user']['username'])
        # self.send_keys(UserSecuritySettingsLocator.verification_code_input, code,doc)
        self.send_keys(AdminSecuritySettingsLocator.verification_code_input, 8888, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_input1, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_input1, Logindata.new_pwd, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_confirm_input1, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_confirm_input1, Logindata.confirm_pwd, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.ok_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.ok_botton, doc)
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data['sit']['admin1']['username'], doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.new_pwd, doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc)
        self.wait_elevisble(Loginlocator.email_verification_code, doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code['sit']['code'], doc)
        self.click_element(Loginlocator.email_submit_ele, doc)
        self.wait_elevisble(Loginlocator.admin_home_page, doc)

        # 改回原密码
        self.enter_pages_admin1()
        time.sleep(5)
        self.wait_elevisble(AdminSecuritySettingsLocator.change_password_botton, doc)
        self.click_element(AdminSecuritySettingsLocator.change_password_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.old_password_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.old_password_input, Logindata.new_pwd, doc)
        time.sleep(2)

        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_input, Logindata.user_data['sit']['admin1']['password'],doc)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.new_password_confirm_input, doc)
        self.send_keys(AdminSecuritySettingsLocator.new_password_confirm_input, Logindata.user_data['sit']['admin1']['password'], doc)
        time.sleep(3)
        self.wait_elevisble(AdminSecuritySettingsLocator.confirm_botton0, doc)
        self.click_element(AdminSecuritySettingsLocator.confirm_botton0, doc)
        time.sleep(5)



    #登录验证
    def login_Athentication(self):
        doc1 = '不启用登录认证功能'
        doc2 = '启用登录认证功能'
        # 不启用认证
        self.enter_pages_admin1()
        time.sleep(5)
        self.wait_elevisble(AdminSecuritySettingsLocator.login_Authentication_botton, doc1)
        self.click_element(AdminSecuritySettingsLocator.login_Authentication_botton, doc1)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.I_acknowledge, doc1)
        self.click_element(AdminSecuritySettingsLocator.I_acknowledge, doc1)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.ok_botton, doc1)
        self.click_element(AdminSecuritySettingsLocator.ok_botton, doc1)
        time.sleep(2)
        self.click_element(AdminSecuritySettingsLocator.avator_botton, doc1)
        time.sleep(2)
        self.click_element(AdminSecuritySettingsLocator.login_out_botton, doc1)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.login_username_ele, doc1)
        self.wait_elevisble(Loginlocator.login_password_ele, doc1)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data['sit']['admin1']['username'], doc1)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data['sit']['admin1']['password'], doc1)
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc1)
        self.wait_elevisble(Loginlocator.admin_home_page, doc1)
        #启用认证
        time.sleep(2)
        self.enter_pages_admin1()
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.login_Authentication_botton, doc2)
        self.click_element(AdminSecuritySettingsLocator.login_Authentication_botton, doc2)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.I_acknowledge, doc2)
        self.click_element(AdminSecuritySettingsLocator.I_acknowledge, doc2)
        time.sleep(2)
        self.wait_elevisble(AdminSecuritySettingsLocator.ok_botton, doc2)
        self.click_element(AdminSecuritySettingsLocator.ok_botton, doc2)
        time.sleep(2)
        self.click_element(AdminSecuritySettingsLocator.avator_botton, doc2)
        time.sleep(2)
        self.click_element(AdminSecuritySettingsLocator.login_out_botton, doc2)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.login_username_ele, doc2)
        self.wait_elevisble(Loginlocator.login_password_ele, doc2)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data['sit']['admin1']['username'], doc2)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data['sit']['admin1']['password'], doc2)
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc2)
        self.wait_elevisble(Loginlocator.email_verification_code, doc2)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code['sit']['code'], doc2)
        self.click_element(Loginlocator.email_submit_ele, doc2)
        self.wait_elevisble(Loginlocator.admin_home_page, doc2)
